home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / MoleculeViewer / XYZApp.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-12-01  |  4.2 KB  |  169 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Event;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.io.InputStream;
  7. import java.net.URL;
  8.  
  9. public class XYZApp extends Applet implements Runnable {
  10.    // $FF: renamed from: md XYZChemModel
  11.    XYZChemModel field_0;
  12.    boolean painted = true;
  13.    float xfac;
  14.    int prevx;
  15.    int prevy;
  16.    float xtheta;
  17.    float ytheta;
  18.    float scalefudge = 1.0F;
  19.    Matrix3D amat = new Matrix3D();
  20.    Matrix3D tmat = new Matrix3D();
  21.    String mdname;
  22.    String message;
  23.    Image backBuffer;
  24.    Graphics backGC;
  25.  
  26.    public void init() {
  27.       this.mdname = ((Applet)this).getParameter("model");
  28.  
  29.       try {
  30.          this.scalefudge = Float.valueOf(((Applet)this).getParameter("scale"));
  31.       } catch (Exception var1) {
  32.       }
  33.  
  34.       this.amat.yrot((double)20.0F);
  35.       this.amat.xrot((double)20.0F);
  36.       if (this.mdname == null) {
  37.          this.mdname = "model.obj";
  38.       }
  39.  
  40.       ((Applet)this).resize(((Component)this).size().width <= 20 ? 400 : ((Component)this).size().width, ((Component)this).size().height <= 20 ? 400 : ((Component)this).size().height);
  41.    }
  42.  
  43.    public void run() {
  44.       InputStream var1 = null;
  45.  
  46.       try {
  47.          Thread.currentThread().setPriority(1);
  48.          var1 = (new URL(((Applet)this).getDocumentBase(), this.mdname)).openStream();
  49.          XYZChemModel var2 = new XYZChemModel(var1);
  50.          Atom.setApplet(this);
  51.          this.field_0 = var2;
  52.          var2.findBB();
  53.          float var3 = var2.xmax - var2.xmin;
  54.          float var4 = var2.ymax - var2.ymin;
  55.          float var5 = var2.zmax - var2.zmin;
  56.          if (var4 > var3) {
  57.             var3 = var4;
  58.          }
  59.  
  60.          if (var5 > var3) {
  61.             var3 = var5;
  62.          }
  63.  
  64.          float var6 = (float)((Component)this).size().width / var3;
  65.          float var7 = (float)((Component)this).size().height / var3;
  66.          this.xfac = 0.7F * (var6 < var7 ? var6 : var7) * this.scalefudge;
  67.          this.backBuffer = ((Component)this).createImage(((Component)this).size().width, ((Component)this).size().height);
  68.          this.backGC = this.backBuffer.getGraphics();
  69.       } catch (Exception var9) {
  70.          ((Throwable)var9).printStackTrace();
  71.          this.field_0 = null;
  72.          this.message = ((Throwable)var9).toString();
  73.       }
  74.  
  75.       try {
  76.          if (var1 != null) {
  77.             var1.close();
  78.          }
  79.       } catch (Exception var8) {
  80.       }
  81.  
  82.       ((Component)this).repaint();
  83.    }
  84.  
  85.    public void start() {
  86.       if (this.field_0 == null && this.message == null) {
  87.          (new Thread(this)).start();
  88.       }
  89.  
  90.    }
  91.  
  92.    public void stop() {
  93.    }
  94.  
  95.    public boolean mouseDown(Event var1, int var2, int var3) {
  96.       this.prevx = var2;
  97.       this.prevy = var3;
  98.       return true;
  99.    }
  100.  
  101.    public boolean mouseDrag(Event var1, int var2, int var3) {
  102.       this.tmat.unit();
  103.       float var4 = (float)(this.prevy - var3) * (360.0F / (float)((Component)this).size().width);
  104.       float var5 = (float)(var2 - this.prevx) * (360.0F / (float)((Component)this).size().height);
  105.       this.tmat.xrot((double)var4);
  106.       this.tmat.yrot((double)var5);
  107.       this.amat.mult(this.tmat);
  108.       if (this.painted) {
  109.          this.painted = false;
  110.          ((Component)this).repaint();
  111.       }
  112.  
  113.       this.prevx = var2;
  114.       this.prevy = var3;
  115.       return true;
  116.    }
  117.  
  118.    public void update(Graphics var1) {
  119.       if (this.backBuffer == null) {
  120.          var1.clearRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
  121.       }
  122.  
  123.       this.paint(var1);
  124.    }
  125.  
  126.    public void paint(Graphics var1) {
  127.       if (this.field_0 != null) {
  128.          this.field_0.mat.unit();
  129.          this.field_0.mat.translate(-(this.field_0.xmin + this.field_0.xmax) / 2.0F, -(this.field_0.ymin + this.field_0.ymax) / 2.0F, -(this.field_0.zmin + this.field_0.zmax) / 2.0F);
  130.          this.field_0.mat.mult(this.amat);
  131.          this.field_0.mat.scale(this.xfac, -this.xfac, 16.0F * this.xfac / (float)((Component)this).size().width);
  132.          this.field_0.mat.translate((float)(((Component)this).size().width / 2), (float)(((Component)this).size().height / 2), 8.0F);
  133.          this.field_0.transformed = false;
  134.          if (this.backBuffer != null) {
  135.             this.backGC.setColor(((Component)this).getBackground());
  136.             this.backGC.fillRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
  137.             this.field_0.paint(this.backGC);
  138.             var1.drawImage(this.backBuffer, 0, 0, this);
  139.          } else {
  140.             this.field_0.paint(var1);
  141.          }
  142.  
  143.          this.setPainted();
  144.       } else {
  145.          if (this.message != null) {
  146.             var1.drawString("Error in model:", 3, 20);
  147.             var1.drawString(this.message, 10, 40);
  148.          }
  149.  
  150.       }
  151.    }
  152.  
  153.    private synchronized void setPainted() {
  154.       this.painted = true;
  155.       this.notifyAll();
  156.    }
  157.  
  158.    private synchronized void waitPainted() {
  159.       while(!this.painted) {
  160.          try {
  161.             this.wait();
  162.          } catch (Exception var1) {
  163.          }
  164.       }
  165.  
  166.       this.painted = false;
  167.    }
  168. }
  169.